package cn.edu.bjut.homework.chapter6;

import cn.edu.bjut.homework.chapter3.Date;
import cn.edu.bjut.homework.chapter4.Cpc;
import cn.edu.bjut.homework.chapter4.Ipc;
import cn.edu.bjut.homework.chapter5.Patent;

import java.util.*;

public class Corpus {
    // protected List<Patent> patentList;
    public Map<Integer, Patent> patentList;

    public Corpus() {
        this.patentList = new HashMap<>();
    }

    public Corpus(HashMap<Integer, Patent> patentList) {
        this.patentList = patentList;
    }

    public Corpus(Patent[] patents) {
        this();
        // this.patentList.addAll(Arrays.asList(patents));
        for (Patent p : patents) {
            this.patentList.put(p.getId(), p);
        }
    }

    public void add(Patent p) {
        this.patentList.put(p.getId(), p);
    }

    public Corpus searchPatent(int id) {
        Corpus result = new Corpus();
        if (patentList.get(id) != null) {
            result.add(patentList.get(id));
        }
        return result;
    }

    public Corpus searchPatent(Date date) {
        Corpus result = new Corpus();
        for (Map.Entry<Integer, Patent> entry : patentList.entrySet()) {
            if (entry.getValue().getDate().equals(date)) {
                result.add(entry.getValue());
            }
        }
        return result;
    }

    public Corpus searchPatent(Ipc ipc) {
        Corpus result = new Corpus();
        for (Map.Entry<Integer, Patent> entry : patentList.entrySet()) {
            if (entry.getValue().getIpcs().contains(ipc)) {
                result.add(entry.getValue());
            }
        }
        return result;
    }

    public Corpus searchPatent(Cpc cpc) {
        Corpus result = new Corpus();
        for (Map.Entry<Integer, Patent> entry : patentList.entrySet()) {
            if (entry.getValue().getCpcs().contains(cpc)) {
                result.add(entry.getValue());
            }
        }
        return result;
    }

    public void deletePatent(int id) {
        patentList.remove(id);

    }

    public void deletePatent(Date date) {
        List<Integer> ids = new ArrayList<>();
        for (Map.Entry<Integer, Patent> entry : patentList.entrySet()) {
            if (entry.getValue().getDate().equals(date)) {
                ids.add(entry.getKey());
            }
        }
        for (Integer id : ids) {
            patentList.remove(id);
        }
    }

    public void deletePatent(Ipc ipc) {
        List<Integer> ids = new ArrayList<>();
        for (Map.Entry<Integer, Patent> entry : patentList.entrySet()) {
            if (entry.getValue().getIpcs().contains(ipc)) {
                ids.add(entry.getKey());
            }
        }
        for (Integer id : ids) {
            patentList.remove(id);
        }
    }

    public void deletePatent(Cpc cpc) {
        List<Integer> ids = new ArrayList<>();
        for (Map.Entry<Integer, Patent> entry : patentList.entrySet()) {
            if (entry.getValue().getCpcs().contains(cpc)) {
                ids.add(entry.getKey());
            }
        }
        for (Integer id : ids) {
            patentList.remove(id);
        }
    }

    @Override
    public String toString() {
        return "Corpus{" +
                "patentList=" + patentList +
                '}';
    }
}
